'use strict';
App.controller('mediasController', ['$rootScope', '$scope', '$state', 'Page','$location', function($rootScope, $scope, $state, Page, $location) {
	Page.ajaxPostQuiet('anon/markType/tree.do', null, function (resp) {
        $scope.marks = resp.data;
    });
	
	
	$scope.mediaBusQuery = {
		item_mark : {},
		item_level : {},
		selectMark : function(parent,mark){
			this.item_mark[parent.markName] = mark || {};
			this.query();
		},
		selectLevel : function(level){
			this.item_level = level || {};
			this.query();
		},
		hasSelectMark : function(){
			var flag = false;
			$.each(this.item_mark,function(idx,mark){
				if(mark.markId && mark.markId != ''){
					flag = true;
					return;
				}
			});
			return flag;
		},
		expendMarkItem : function(iid){
			var area = $('#' + iid)
			if (area.hasClass("search-item-area-collapse")) {
	            area.removeClass("search-item-area-collapse");
	        } else {
	            area.addClass("search-item-area-collapse");
	        }
		},
		cancelMark : function(mark){
			var self = this;
			$.each(this.item_mark,function(idx,_mark){
				if(_mark.markId && mark.markId == _mark.markId){
					self.item_mark[idx] = {};
					return;
				}
			});
		},
		cancelLevel : function(){
			this.item_level = {};
		},
		query : function(){
			var self = this;
			var param = {};
			param.pageIndex = 0;
			param.pageSize = 10;
			param.mediaMarks = [];
			$.each(this.item_mark,function(idx,mark){
				if(mark.markId && mark.markId != ''){
					param.mediaMarks.push(mark.markId);
				}
			});
			param.mediaLevel = this.item_level.value;
			Page.ajaxPost('anon/www/media/lines.do',param,function(resp){
				if(!resp.data || !resp.data.rows){
					return;
				}
		    	self.datas = resp.data.rows;
		    	// 调用分页
		    	self.totalPages = resp.data.total/param.pageSize + (resp.data.total%param.pageSize == 0 ? 0 : 1);
				laypage({
					cont : 'bus_page_bar',
					pages : self.totalPages,// 得到总页数
					skin : '#3E97E4',
					skip : true, // 是否开启跳页
					prev : '<', // 若不显示，设置false即可
					next : '>', // 若不显示，设置false即可
					groups : 5, //连续分页数
					curr: 1, //初始化当前页
					jump : function(obj,first) {// 触发分页后的回调
						/*obj是一个object类型。包括了分页的所有配置信息。
						 first一个Boolean类，检测页面是否初始加载。非常有用，可避免无限刷新。*/
						if(!first){
							param.pageIndex = obj.curr - 1;
							Page.ajaxPost('anon/www/media/lines.do', param, function(resp) {
								if(!resp.data || !resp.data.rows){
									return;
								}
								self.datas = resp.data.rows;
								self.totalPages = resp.data.total/param.pageSize + (resp.data.total%param.pageSize == 0 ? 0 : 1);
							});
						}
					}
				})
		    });
		},
		datas : [
			//{lineName: '15路',level:'A+', busNum : '13', price:'',marks : ['A','B','C','G','H'],ss:'',es:''}
		]
		
	};
	
	$scope.mediaStopQuery = {
		item_mark : {},
		item_level : {},
		selectMark : function(parent,mark){
			this.item_mark[parent.markName] = mark || {};
			this.query();
		},
		selectLevel : function(level){
			this.item_level = level || {};
			this.query();
		},
		hasSelectMark : function(){
			var flag = false;
			$.each(this.item_mark,function(idx,mark){
				if(mark.markId && mark.markId != ''){
					flag = true;
					return;
				}
			});
			return flag;
		},
		expendMarkItem : function(iid){
			var area = $('#' + iid)
			if (area.hasClass("search-item-area-collapse")) {
	            area.removeClass("search-item-area-collapse");
	        } else {
	            area.addClass("search-item-area-collapse");
	        }
		},
		cancelMark : function(mark){
			var self = this;
			$.each(this.item_mark,function(idx,_mark){
				if(_mark.markId && mark.markId == _mark.markId){
					self.item_mark[idx] = {};
					return;
				}
			});
		},
		cancelLevel : function(){
			this.item_level = {};
		},
		query : function(){
			var self = this;
			var param = {};
			param.pageIndex = 0;
			param.pageSize = 10;
			param.mediaMarks = [];
			$.each(this.item_mark,function(idx,mark){
				if(mark.markId && mark.markId != ''){
					param.mediaMarks.push(mark.markId);
				}
			});
			param.mediaLevel = this.item_level.value;
			Page.ajaxPost('anon/www/media/stops.do',param,function(resp){
				if(!resp.data || !resp.data.rows){
					return;
				}
		    	self.datas = resp.data.rows;
		    	// 调用分页
		    	self.totalPages = resp.data.total/param.pageSize + (resp.data.total%param.pageSize == 0 ? 0 : 1);
				laypage({
					cont : 'stop_page_bar',
					pages : self.totalPages,// 得到总页数
					skin : '#3E97E4',
					skip : true, // 是否开启跳页
					prev : '<', // 若不显示，设置false即可
					next : '>', // 若不显示，设置false即可
					groups : 5, //连续分页数
					curr: 1, //初始化当前页
					jump : function(obj,first) {// 触发分页后的回调
						/*obj是一个object类型。包括了分页的所有配置信息。
						 first一个Boolean类，检测页面是否初始加载。非常有用，可避免无限刷新。*/
						if(!first){
							param.pageIndex = obj.curr - 1;
							Page.ajaxPost('anon/www/media/stops.do', param, function(resp) {
								if(!resp.data || !resp.data.rows){
									return;
								}
								self.datas = resp.data.rows;
								self.totalPages = resp.data.total/param.pageSize + (resp.data.total%param.pageSize == 0 ? 0 : 1);
							});
						}
					}
				})
		    });
		},
		datas : [
			//{lineName: '15路',level:'A+', busNum : '13', price:'',marks : ['A','B','C','G','H'],ss:'',es:''}
		]
		
	};
	
	$scope.mediaOtherQuery = {
		item_mark : {},
		item_level : {},
		selectMark : function(parent,mark){
			this.item_mark[parent.markName] = mark || {};
			this.query();
		},
		selectLevel : function(level){
			this.item_level = level || {};
			this.query();
		},
		hasSelectMark : function(){
			var flag = false;
			$.each(this.item_mark,function(idx,mark){
				if(mark.markId && mark.markId != ''){
					flag = true;
					return;
				}
			});
			return flag;
		},
		expendMarkItem : function(iid){
			var area = $('#' + iid)
			if (area.hasClass("search-item-area-collapse")) {
	            area.removeClass("search-item-area-collapse");
	        } else {
	            area.addClass("search-item-area-collapse");
	        }
		},
		cancelMark : function(mark){
			var self = this;
			$.each(this.item_mark,function(idx,_mark){
				if(_mark.markId && mark.markId == _mark.markId){
					self.item_mark[idx] = {};
					return;
				}
			});
		},
		cancelLevel : function(){
			this.item_level = {};
		},
		query : function(){
			var self = this;
			var param = {};
			param.pageIndex = 0;
			param.pageSize = 10;
			param.mediaMarks = [];
			$.each(this.item_mark,function(idx,mark){
				if(mark.markId && mark.markId != ''){
					param.mediaMarks.push(mark.markId);
				}
			});
			param.mediaLevel = this.item_level.value;
			Page.ajaxPost('anon/www/media/others.do',param,function(resp){
				if(!resp.data || !resp.data.rows){
					return;
				}
		    	self.datas = resp.data.rows;
		    	// 调用分页
		    	self.totalPages = resp.data.total/param.pageSize + (resp.data.total%param.pageSize == 0 ? 0 : 1);
				laypage({
					cont : 'other_page_bar',
					pages : self.totalPages,// 得到总页数
					skin : '#3E97E4',
					skip : true, // 是否开启跳页
					prev : '<', // 若不显示，设置false即可
					next : '>', // 若不显示，设置false即可
					groups : 5, //连续分页数
					curr: 1, //初始化当前页
					jump : function(obj,first) {// 触发分页后的回调
						/*obj是一个object类型。包括了分页的所有配置信息。
						 first一个Boolean类，检测页面是否初始加载。非常有用，可避免无限刷新。*/
						if(!first){
							param.pageIndex = obj.curr - 1;
							Page.ajaxPost('anon/www/media/others.do', param, function(resp) {
								if(!resp.data || !resp.data.rows){
									return;
								}
								self.datas = resp.data.rows;
								self.totalPages = resp.data.total/param.pageSize + (resp.data.total%param.pageSize == 0 ? 0 : 1);
							});
						}
					}
				})
		    });
		},
		datas : [
			//{lineName: '15路',level:'A+', busNum : '13', price:'',marks : ['A','B','C','G','H'],ss:'',es:''}
		]
		
	};
	
	$('body').on('click', 'a.search-order-expand,a.search-order-collapse,.search-item-expand,.search-item-collapse', function(e) {
        e.preventDefault();
        if ($(this).hasClass("search-order-collapse")) {
            $(this).removeClass("search-order-collapse").addClass("search-order-expand");
        } else {
            $(this).removeClass("search-order-expand").addClass("search-order-collapse");
        }
    });
	
	$scope.search = $location.search().search || 'bus';
	if($scope.search == 'other'){
		$scope.mediaOtherQuery.active = true;
		$scope.mediaOtherQuery.query();
	}else if($scope.search == 'stop'){
		$scope.mediaStopQuery.active = true;
		$scope.mediaStopQuery.query();
	}else {
		$scope.mediaBusQuery.active = true;
		$scope.mediaBusQuery.query();
	}
	
}]);